package com.fndsoft.police.xuancheng.data;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.os.Environment;

import com.fndsoft.police.xuancheng.domain.BaseDomain;
import com.fndsoft.police.xuancheng.domain.BaseObject;
import com.fndsoft.police.xuancheng.ui.base.BaseApp;
import com.fndsoft.police.xuancheng.util.ConfigUtil;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.DbUtils.DbUpgradeListener;
import com.lidroid.xutils.exception.DbException;

/**
 * @author carlos carlosk@163.com
 * @version 创建时间：2012-9-30 上午6:51:22 类说明
 */

public class BaseData extends BaseObject {
	public static DbUtils db = null;
	private static final String DB_NAME = "meap_zhe_shang.db";
	private static final int DB_VERSION = 1;

	public static final String PACKAGE_NAME = "com.fndsoft.police.xuancheng"; // 这个是自己项目包路径
	public static final String DATABASE_PATH = "/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ PACKAGE_NAME + "/databases"; // 获取存储位置地址

	static {
		InputStream is = null;
		FileOutputStream fos = null;
		try {

			// 当第一次建立数据库时从raw中导入数据
			// String databaseFilename = DATABASE_PATH + "/" + DB_NAME;
			// File dir = new File(DATABASE_PATH);
			// if (!dir.exists()) {
			// dir.mkdir();
			// }
			// if (!(new File(databaseFilename)).exists()) {
			// is = BaseApp.AppContext.getResources().openRawResource(
			// R.raw.meap_zhe_shang);
			// fos = new FileOutputStream(databaseFilename);
			// byte[] buffer = new byte[8192];
			// int count = 0;
			// while ((count = is.read(buffer)) > 0) {
			// fos.write(buffer, 0, count);
			// }
			// }

			db = DbUtils.create(BaseApp.AppContext, DB_NAME, DB_VERSION,
					new DbUpgradeListener() {
						@Override
						public void onUpgrade(DbUtils db, int oldVersion,
								int newVersion) {

						}
					});
			if (ConfigUtil.isPrintLog()) {
				db.configDebug(true);
			}

			//db.createTableIfNotExist(TaskInfo.class);
//		} catch (DbException e) {
//			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != fos) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (null != is) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 开启事务
	 * 
	 * @author liujiaqi
	 * @creation time 2014-3-25上午9:14:17
	 * @version
	 */
	public static void beginTransaction() {
		db.getDatabase().beginTransaction();
	}

	/**
	 * 完成并结束事务
	 * 
	 * @author liujiaqi
	 * @creation time 2014-3-25上午9:14:28
	 * @version
	 */
	public static void endTransaction() {
		db.getDatabase().setTransactionSuccessful();
		db.getDatabase().endTransaction();
	}

	/**
	 * 根据实体对象保存到数据表，保存的时候会让“@Id”自增
	 * 
	 * @author liujiaqi
	 * @creation time 2014-3-21下午5:37:29
	 * @version
	 * @param domain
	 * @throws Exception
	 */
	public static void insert(BaseDomain domain) throws Exception {
		db.saveBindingId(domain);
	}

	/**
	 * 更新，需要保证"@Id"列有值
	 * 
	 * @author liujiaqi
	 * @creation time 2014-3-21下午5:38:43
	 * @version
	 * @param domain
	 * @throws Exception
	 */
	public static void update(BaseDomain domain) throws Exception {
		db.saveOrUpdate(domain);
	}

	/**
	 * 删除，需要保证"@Id"列有值
	 * 
	 * @author liujiaqi
	 * @creation time 2014-3-21下午5:39:27
	 * @version
	 * @param domain
	 * @throws Exception
	 */
	public static void del(BaseDomain domain) throws Exception {
		db.delete(domain);
	}

	/**
	 * 根据id查询实体
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-4-15 下午6:27:15
	 * @version
	 * @param entityType
	 * @param idValue
	 * @return
	 * @throws DbException
	 */
	public static <T> T findById(Class<T> entityType, Object idValue)
			throws DbException {
		return BaseData.db.findById(entityType, idValue);
	}
}
